package com.aletto.general_store.presenters;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aletto.general_store.R;
import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.model.OrderNumber;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;
import com.sisyphus.spinach.ui.configs.impl.DefaultTemplate;

public class OrderNumberTemplate extends DefaultTemplate<OrderNumber> {

    public OrderNumberTemplate(ViewGroup parent) {
        super(parent);
    }

    @Override
    public INetTask.View<OrderNumber> view() {
        return new INetTask.View<>() {

            @Override
            public void success(OrderNumber data) {

                if (mParent == null) {
                    return;
                }

                TextView tv_store_delivery_number = mParent.findViewById(R.id.tv_store_delivery_number);
                TextView tv_store_shipped_number = mParent.findViewById(R.id.tv_store_shipped_number);
                TextView tv_store_after_sale_number = mParent.findViewById(R.id.tv_store_after_sale_number);
                TextView tv_store_complete_number = mParent.findViewById(R.id.tv_store_complete_number);

                // 个人店铺
                OrderNumber.ShopOrderDTO shopOrder = data.getShop_order();
                if (shopOrder != null) {
                    tv_store_delivery_number.setText(String.valueOf(shopOrder.getTreat_num())); // 待发货
                    tv_store_shipped_number.setText(String.valueOf(shopOrder.getSend_num())); // 已发货
                    tv_store_after_sale_number.setText(String.valueOf(shopOrder.getRefund_num())); // 售后中
                    tv_store_complete_number.setText(String.valueOf(shopOrder.getComplete_num())); // 已完成
                }

                // 我的订单
                OrderNumber.MyOrderDTO myOrder = data.getMy_order();
                if (myOrder != null) {

                    TextView tv_trade_delivery_number = mParent.findViewById(R.id.tv_trade_delivery_number); // 待发货
                    TextView tv_shipped_number = mParent.findViewById(R.id.tv_shipped_number); // 待收货
                    TextView tv_trade_subsequent_number = mParent.findViewById(R.id.tv_trade_subsequent_number); // 退款&售后

                    setNumber(tv_trade_delivery_number, myOrder.getTreat_num());
                    setNumber(tv_shipped_number, myOrder.getSend_num());
                    setNumber(tv_trade_subsequent_number, myOrder.getComplete_num());
                }

            }

        };
    }

    private void setNumber(TextView textView, int number) {
        if (number != 0) {
            textView.setVisibility(View.VISIBLE);
            textView.setText(String.valueOf(number));

        } else {
            textView.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public INetTask.Presenter<OrderNumber, INetTask.View<OrderNumber>> presenter() {
        return new NetSingleTask<>(DataRepository.getInstance().myOrderInfo());
    }

}
